var timestamp = Date.parse(new Date());

var oldHandleMesageHook;
var curSetHookCount = 0;
var regHookCount = 0;

function setHandleMessageHookForWeixin() {
    try {
        if (curSetHookCount > 15) {
            return;
        }

        if (!window.WeixinJSBridge) {
            setTimeout("setHandleMessageHookForWeixin()", 1000);
            curSetHookCount++;
            return;
        }



        if (!oldHandleMesageHook) {
            oldHandleMesageHook = window.WeixinJSBridge._handleMessageFromWeixin;
            window.WeixinJSBridge._handleMessageFromWeixin = function(message) {
                try {
                    var realMessage = message['__json_message'];
                    var shaStr = message['__sha_key'];
                    var eventId = realMessage['__event_id'];
                    var msgType = realMessage['__msg_type'];
                    var callbackId = realMessage['__callback_id'];
                    if (eventId && eventId.indexOf("share") > 0) {

                        var eventMsg = "sendAppMessage";
                        var tmstr = eventId;

                        if (eventId == "general:share") {
                            var params = realMessage['__params'];
                            tmstr = params['shareTo'];
                        }
                        if (tmstr.indexOf("timeline") != -1) {
                            eventMsg = "shareTimeline";
                        }
                        if (eventMsg) {
                            var link = getShareLink();
                            var share_data = {
                                "link": link,
                                "title": getShareTitle(),
                                "desc": getShareDesc(),
                                "img_url": getShareImageUrl()
                            };
                            window.WeixinJSBridge.invoke(eventMsg, share_data, shareCallback);
                            restoreHandleMessageHookForWeixin();
                        }
                    }
                } catch (e) {}
            }
        }

        regHookCount++;
    } catch (e) {}
}

function restoreHandleMessageHookForWeixin() {
    if (oldHandleMesageHook) {
        window.WeixinJSBridge._handleMessageFromWeixin = oldHandleMesageHook;
    }
}

function shareCallback(res) {
    var errMsg = res['err_msg'];
    if (errMsg) {
        if (errMsg.indexOf(":confirm") != -1 || errMsg.indexOf(":ok") != -1) {
            shareComplete();
        } else {}
    }

    curSetHookCount = 0;
    oldHandleMesageHook = undefined;
    setHandleMessageHookForWeixin();
}

var count = 0;

function shareComplete() {
    count++;
    if (count < 1) {} else {
        switch (count) {
            case 1:
                if(getShareNextUrl() != null){
                    localStorage.share = 1;
                    // showmsg('发送成功,请再发送2个不同的微信群即可领取！');
                }
                break;
            case 2:
                if(getShareNextUrl() != null) {
                    localStorage.share = 2;
                    // showmsg('发送成功,请再发送1个不同的微信群即可领取！');
                }
                break;
            case 3:
                var jump_url = getShareNextUrl();
                if(jump_url){
                    location.href = jump_url;
                }
                break;
        }

        shareCompleteOut(count);
        // if (typeof(eval("shareCompleteOut")) == "function") {
        //     shareCompleteOut(count);
        // }

    }
}
setHandleMessageHookForWeixin();